package iss.java;

/**
 * Created by WeiZehao on 16/10/9.
 */
public class StudentList
{
    private Student[] _students;        //学生
    private double[] _averageGrades;    //平均成绩
    private int _number;                //学生数量

    public StudentList(int count)
    {
        _students = new Student[count];
        _averageGrades = new double[5];
        _number = 0;
    }

    public Student[] get_students()
    {
        return _students;
    }

    public double[] get_averageGrades()
    {
        return _averageGrades;
    }

    /*添加学生*/
    public void addStudent(String name, String id, double ch,
                           double ma, double en, double phy, double che)
    {
        Student student = new Student(name, id);
        student.set_grades(ch,ma,en,phy,che);
        _students[_number++] = student;
    }

    /*计算平均成绩*/
    public void calculateAverage()
    {
        for (double j : _averageGrades)
        {
            j = 0.0;
        }
        for(int i = 0; i < 5; i++)
        {
            for(int j = 0; j < _students.length; j++)
            {
                    _averageGrades[i] = _averageGrades[i]
                            + (_students[j].get_grades()[i]/_students.length);
            }
        }
    }

    /*按照总评成绩排序，归并排序法*/
    /*合并数组*/
    private void merge(int head, int mid, int end)
    {
        // 被排序部分长度
        int length = end - head + 1;
        // 临时数组，用来储存合并后的数组
        Student[] arr= new Student[length];
        // 右数组的第一个索引
        int head_right = mid + 1;
        // 临时数组索引
        int i;
        // 储存左数组第一个索引, 将临时数组值赋给原数组时要用到
        int start = head;

        // 从左右两部分数组中取出总评成绩大的元素放入临时数组中
        for(i = 0; head <= mid && head_right <= end; i++)
        {
            if(_students[head].get_totalMark() > _students[head_right].get_totalMark())
            {
                arr[i] = _students[head];
                head++;
            }
            else
            {
                arr[i] = _students[head_right];
                head_right++;
            }
        }

        // 若左数组有剩余，将剩余部分放入临时数组中
        while(head <= mid)
        {
            arr[i++] = _students[head++];
        }
        // 若右数组有剩余，将剩余部分放入临时数组中
        while(head_right <= end)
        {
            arr[i++] = _students[head_right++];
        }

        // 将临时数组的值赋给原数组
        for (int j = 0; j < length; )
        {
            _students[start++] = arr[j++];
        }
    }
    /*排序数组*/
    public void mergeSort(int head, int end)
    {
        //当所排数组长度大于1时才执行下面内容
        if(head < end)
        {
            // 获取中间位置，以此把当前数组分割为左右两数组
            int mid = (head + end) / 2;
            // 排序左数组
            mergeSort(head, mid);
            // 排序右数组
            mergeSort(mid + 1, end);
            // 合并左右数组
            merge(head, mid, end);
        }
    }

    /*向控制台输出学生信息*/
    public void showStudentInConsole()
    {
        for(int i = 0; i < 5; i++)
        {
            System.out.print(_students[i].get_name() + " ");
            System.out.print(_students[i].get_id() + " ");
            for (double j : _students[i].get_grades())
            {
                System.out.print(j + " ");
            }
            System.out.print(_students[i].get_totalMark() + " ");
            System.out.println();
        }
    }
}// end class StudentList
